package com.xhcl.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xhcl.system.mapper.TSrhsZcfzMapper;
import com.xhcl.system.model.SubstanceDebtAll;
import com.xhcl.system.domain.SubstanceDebt;
import com.xhcl.system.domain.TSrhsZcfz;
import com.xhcl.system.service.ITSrhsZcfzService;
import com.xhcl.system.utils.MathUtil;
import com.xhcl.common.core.text.Convert;
import com.xhcl.common.exception.BusinessException;

/**
 * 资产负债Service业务层处理
 * 
 * @author xhcl
 * @date 2020-09-04
 */
@Service
public class TSrhsZcfzServiceImpl implements ITSrhsZcfzService 
{
    @Autowired
    private TSrhsZcfzMapper tSrhsZcfzMapper;

    /**
     * 查询资产负债
     * 
     * @param num 资产负债ID
     * @return 资产负债
     */
    @Override
    public TSrhsZcfz selectTSrhsZcfzById(Long num)
    {
        return tSrhsZcfzMapper.selectTSrhsZcfzById(num);
    }

    /**
     * 查询资产负债列表
     * 
     * @param tSrhsZcfz 资产负债
     * @return 资产负债
     */
    @Override
    public List<TSrhsZcfz> selectTSrhsZcfzList(TSrhsZcfz tSrhsZcfz)
    {
        return tSrhsZcfzMapper.selectTSrhsZcfzList(tSrhsZcfz);
    }

    /**
     * 新增资产负债
     * 
     * @param tSrhsZcfz 资产负债
     * @return 结果
     */
    @Override
    public int insertTSrhsZcfz(TSrhsZcfz tSrhsZcfz)
    {
        return tSrhsZcfzMapper.insertTSrhsZcfz(tSrhsZcfz);
    }

    /**
     * 修改资产负债
     * 
     * @param tSrhsZcfz 资产负债
     * @return 结果
     */
    @Override
    public int updateTSrhsZcfz(TSrhsZcfz tSrhsZcfz)
    {
        return tSrhsZcfzMapper.updateTSrhsZcfz(tSrhsZcfz);
    }

    /**
     * 删除资产负债对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTSrhsZcfzByIds(String ids)
    {
        return tSrhsZcfzMapper.deleteTSrhsZcfzByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除资产负债信息
     * 
     * @param num 资产负债ID
     * @return 结果
     */
    @Override
    public int deleteTSrhsZcfzById(Long num)
    {
        return tSrhsZcfzMapper.deleteTSrhsZcfzById(num);
    }
    
    public List<SubstanceDebtAll> selectTSrhsZcfzReportList(TSrhsZcfz tSrhsZcfz){

    	List<SubstanceDebtAll> newlist=new ArrayList<SubstanceDebtAll>();
    	List<TSrhsZcfz> alist=tSrhsZcfzMapper.selectTSrhsZcfzReportList(tSrhsZcfz);
    	List<SubstanceDebt> list= getSubstanceDebt(alist);
    	 if (list != null && list.size()>0 )
    	 {
    	      Map map = new HashMap();
    	      SubstanceDebt substanceDebt = null;
    	      for (int i = 0; i < list.size(); i++) {
    	        substanceDebt = (SubstanceDebt)list.get(i);
    	        map.put(substanceDebt.getRownum().intValue(), substanceDebt);
    	      }
    	      SubstanceDebt substanceDebt1 = null;
    	      SubstanceDebt substanceDebt2 = null;
    	      substanceDebt1 = (SubstanceDebt)list.get(list.size()-1);
    	      int maxRownum = substanceDebt1.getRownum().intValue();
    	      if(maxRownum%2!=0){
    	    	  throw new BusinessException("负债表参数设置不对");
    	      }
			  int index = (maxRownum/2);
			  for (int i = 1; i <=index; i++) {
				    SubstanceDebtAll substancedebtall=new SubstanceDebtAll();
				    substanceDebt1 = (SubstanceDebt)map.get(new Integer(i));
					substanceDebt2 = (SubstanceDebt)map.get(new Integer(i+index));
					if(substanceDebt1!=null) {
						substancedebtall.setName1(substanceDebt1.getName());
						substancedebtall.setNum1(i);
						substancedebtall.setNcye1(substanceDebt1.getNcye());
						substancedebtall.setQmye1(substanceDebt1.getQmye());
					}else {
						substancedebtall.setNum1(i);
					}
					if(substanceDebt2!=null) {
						substancedebtall.setName2(substanceDebt2.getName());
						substancedebtall.setNum2(i+index);
						substancedebtall.setNcye2(substanceDebt2.getNcye());
						substancedebtall.setQmye2(substanceDebt2.getQmye());
					}else {
						substancedebtall.setNum2(i+index);
					}
					newlist.add(substancedebtall);
			  }
			
				
    	  }
    	 return newlist; 
    	
    }
    
    public List<SubstanceDebt> getSubstanceDebt(List<TSrhsZcfz> alist)
    {   
    	List<SubstanceDebt> list = new ArrayList();
    	for (TSrhsZcfz zcfz:alist) {
        	SubstanceDebt o = new SubstanceDebt();
            o.setName(zcfz.getText());
            o.setRule(zcfz.getRule());
            o.setRownum(zcfz.getNum());
            o.setIndent(zcfz.getSjs());
            o.setSfjc(zcfz.getSfjc());
            o.setType(zcfz.getStyle());
            o.setNcye(zcfz.getNcye() == null ? new BigDecimal("0") : 
            	zcfz.getNcye());
            o.setQmye(zcfz.getQmye() == null ? new BigDecimal("0") :zcfz.getQmye());
            list.add(o);
        }
    	
        Map<String,Object> map = new HashMap();
        List<SubstanceDebt> total = new ArrayList();
        for (SubstanceDebt o:list) {
        	 map.put(String.valueOf(o.getRownum()), o);
	        if (o.getType() == 3) {
	            total.add(o);
	        }
        }
        for (int i = 0; i < total.size(); i++) {
          SubstanceDebt o = (SubstanceDebt)total.get(i);
          List expression_nc = new ArrayList();
          List expression_qm = new ArrayList();
          int temp = -1;
          int point = 0;
          for (int k = 0; k < o.getRule().length(); k++) {
            point = "+-*/()".indexOf(o.getRule().charAt(k));
            if (point != -1) {
              if (temp + 1 != k) {
                expression_nc.add(
                  ((SubstanceDebt)map.get(o.getRule().substring(temp + 1, k))).getNcye()
                  .toString());
                expression_qm.add(
                  ((SubstanceDebt)map.get(o.getRule().substring(temp + 1, k))).getQmye()
                  .toString());
              }
              expression_nc.add(o.getRule().substring(k, k + 1));
              expression_qm.add(o.getRule().substring(k, k + 1));
              temp = k;
            }
          }
          if (temp < o.getRule().length() - 1) {
            expression_nc.add(
              ((SubstanceDebt)map.get(o.getRule().substring(temp + 1))).getNcye().toString());
            expression_qm.add(
              ((SubstanceDebt)map.get(o.getRule().substring(temp + 1))).getQmye().toString());
          }
          String[] s = new String[expression_nc.size()];
          o.setNcye(new MathUtil().calculate((String[])expression_nc.toArray(s)));
          o.setQmye(new MathUtil().calculate((String[])expression_qm.toArray(s)));
        }
        return list;
    }
}
